﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using BingleMaps.Models;
using BingleMaps.Services;
using BingleMaps.ViewModels;
using Microsoft.Phone.Shell;

namespace BingleMaps.Views
{
	public partial class LayersView
	{
		private readonly MainViewModel _mainViewModel;

		public LayersView()
		{
			InitializeComponent();
			_mainViewModel = (MainViewModel)DataContext;
			AppBarIconButtonDelete = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
			AppBarIconButtonSync = (ApplicationBarIconButton)ApplicationBar.Buttons[1];
		}

		protected override void OnBackKeyPress(CancelEventArgs e)
		{
			if (NavigationService != null && NavigationService.CanGoBack)
			{
				base.OnBackKeyPress(e);
			}
			else
			{
				e.Cancel = true;
			}
		}

		private void AppBarIconButtonDeleteClick(object sender, EventArgs e)
		{
			if (Pivot.SelectedItem == UserPinsPivotItem)
			{
				_mainViewModel.EditingUserPin = null;
				_mainViewModel.UserPinLog.RemoveCheckedPins();
				return;
			}

			if (Pivot.SelectedItem != SearchPivotItem)
			{
				return;
			}

			for (var i = _mainViewModel.TemporaryPins.Count - 1; i >= 0; i--)
			{
				if (_mainViewModel.TemporaryPins[i].IsChecked)
				{
					_mainViewModel.TemporaryPins.RemoveAt(i);
				}
			}
		}

		private void ItineryItemGoHyperlinkButtonClick(object sender, RoutedEventArgs e)
		{
			_mainViewModel.MapCenter = ((KeyValuePair<UInt16, ItineraryItem>)((FrameworkElement)sender).DataContext).Value.Location;
			_mainViewModel.ZoomLevel = 15D;
			NavigationService.GoBack();
		}

		private void PivotSelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (Pivot.SelectedItem == null)
			{
				return;
			}
			if (Pivot.SelectedItem == ItineraryPivotItem)
			{
				AppBarIconButtonDelete.IsEnabled = false;
				AppBarIconButtonSync.IsEnabled = false;
				return;
			}
			if (Pivot.SelectedItem == UserPinsPivotItem)
			{
				AppBarIconButtonDelete.IsEnabled = true;
				AppBarIconButtonSync.IsEnabled = _mainViewModel.Settings.Sync;
				return;
			}
			if (Pivot.SelectedItem == SearchPivotItem)
			{
				AppBarIconButtonDelete.IsEnabled = true;
				AppBarIconButtonSync.IsEnabled = false;
				return;
			}
		}

		private void AppBarMenuItemSettingsClick(object sender, EventArgs e)
		{
			NavigationService.Navigate(new Uri("/Views/MoreView.xaml", UriKind.Relative));
		}

		private void PhoneApplicationPageLoaded(object sender, RoutedEventArgs e)
		{
			if (Pivot.SelectedItem == UserPinsPivotItem)
			{
				AppBarIconButtonSync.IsEnabled = _mainViewModel.Settings.Sync;
			}
			_mainViewModel.EditingUserPin = null;
		}

		private void AppBarIconButtonSyncClick(object sender, EventArgs e)
		{
			if (_mainViewModel.Settings.DropNetUserLogin == null)
			{
				NavigationService.Navigate(new Uri("/Views/DropBoxView.xaml", UriKind.Relative));
				return;
			}
			ProgressBar.IsIndeterminate = true;
			AppBarIconButtonSync.IsEnabled = false;
			DropBoxServices.Sync(_mainViewModel.Settings.DropNetUserLogin, _mainViewModel.UserPinLog,
								 status =>
								 {
									 ProgressBar.IsIndeterminate = false;
									 AppBarIconButtonSync.IsEnabled = true;
								 }, ex =>
								 {
									 if (ex.StatusCode != HttpStatusCode.OK)
									 {
										 MessageBox.Show("There was a problem syncing your data, please try again later.", "Sync Failed", MessageBoxButton.OK);
									 }
									 else
									 {
										 MessageBox.Show("Your data has been synchronised successfully.", "Sync Successful", MessageBoxButton.OK);
									 }
								 });
		}

		private void UserPinListItemEdit(object sender, RoutedEventArgs e)
		{
			_mainViewModel.EditingUserPin = (UserPin)((FrameworkElement)sender).DataContext;
			NavigationService.Navigate(new Uri("/Views/UserPinView.xaml", UriKind.Relative));
		}

		private void UserPinListItemGo(object sender, RoutedEventArgs e)
		{
			_mainViewModel.EditingUserPin = (UserPin)((FrameworkElement)sender).DataContext;
			_mainViewModel.MapCenter = _mainViewModel.EditingUserPin.Location;
			_mainViewModel.ZoomLevel = 15D;
			NavigationService.GoBack();
		}
	}
}
